package servlet.huwenyao;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import pojo.Order;
import pojo.User;
import service.OrderService;
import service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/ExportOrderServlet")
public class ExportOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        request.setCharacterEncoding("UTF-8");
        String fids = request.getParameter("ids");
        if (null != fids && !("".equals(fids))) {
            String[] fid = fids.split(",");
            List<Order> list = new ArrayList<Order>();
            //存储参数
            OrderService fs = new OrderServiceImpl();
            for (String id : fid) {
                list.add(fs.finaOrder(Integer.parseInt(id)));
            }
            String[] title = {"用户账号", "商品id", "订单总价", "订单状态", "订单备注", "订单地址", "订单生成时间"};

            //创建Excel工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个工作表
            HSSFSheet sheet = workbook.createSheet();
            //创建第一行  sheet.createRow(0)表示第一行
            HSSFRow row = sheet.createRow(0);
            //列
            HSSFCell cell = null;
            for (int i = 0; i < title.length; i++) {
                //循环往第一行列中进行赋值
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
            }
            SimpleDateFormat s=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
            List<User> userList = new ArrayList<User>();
            //循环创建其他行并对每行的列增加数据
            for (int i = 0; i < list.size(); i++) {
                //通过订单信息的用户id获取用户对象并保存在userList集合中
                String state=String.valueOf(list.get(i).getOrderState());

                switch (list.get(i).getOrderState()){
                    case 0: state="已完成";break;
                    case 1: state="待付款";break;
                    case 2: state="待发货";break;
                    case 3: state="待收货";break;
                    case 4: state="待评论";break;
                }
                    userList.add(fs.finaUser(list.get(i).getConsumerId()));
                    HSSFRow nextrow = sheet.createRow(i + 1);
                    HSSFCell dataCell = nextrow.createCell(0);
                    dataCell.setCellValue(userList.get(i).getuAccount());
                    dataCell = nextrow.createCell(1);
                    dataCell.setCellValue(list.get(i).getWareId());
                    dataCell = nextrow.createCell(2);
                    dataCell.setCellValue(list.get(i).getOrderPrice());
                    dataCell = nextrow.createCell(3);
                    dataCell.setCellValue(state);
                    dataCell = nextrow.createCell(4);
                    dataCell.setCellValue(list.get(i).getOrderRemarks());
                    dataCell = nextrow.createCell(5);
                    dataCell.setCellValue(list.get(i).getOrderAddress());
                    dataCell = nextrow.createCell(6);
                    dataCell.setCellValue(s.format(list.get(i).getOrderDate()));

            }
            OutputStream output = response.getOutputStream();
            response.reset();
            //导入路径,选择
            response.setHeader("Content-disposition", "attachment; filename=customers.xls");
            response.setContentType("application/msexcel");
            workbook.write(output);
            output.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}